"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectGatheringCtrl
 * @description
 * # ProjectGatheringCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectGatheringCtrl", function (
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);

    // 选择款项用途
    $scope.getcostTypeObj = function () {
      var param = {};
      param.type = "项目款项小类";
      param.parent = $scope.costTypeList.filter(function (item) {
        return item.value == $scope.param.receiveType;
      })[0].key;
      Util.getKeyValue(param, function (data) {
        $scope.receiveSubTypeList = data.data;
      });
    };
    $scope.costTypechange = function () {
      $scope.param.receiveSubType = "";
      $scope.param.peasantWorkerDeposit = "";
      $scope.getcostTypeObj();
    };
    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.chooseInvoice = function () {
      var params = $scope.param;
      if (!params.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      var projectParams = {
        projectId: params.projectId,
        formKey: "Xm_OutputInvoice",
        fieldName: "outputInvoiceNo"
        // keyWrod: "Xm_ProjectPayment"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectGathering;
      options.param = angular.copy($scope.param, {});
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.payer) {
        Util.Toast("请填写交款人");
        return;
      }
      if (!$scope.param.receiveDate) {
        Util.Toast("请选择交款日期");
        return;
      }
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (!$scope.param.targetAccount) {
        Util.Toast("请选择收款账户");
        return;
      }
      if (!$scope.param.receiveType) {
        Util.Toast("请选择项目类别");
        return;
      }
      if (!$scope.param.amount) {
        Util.Toast("请填写收款金额");
        return;
      }
      if (!$scope.param.receiveReason) {
        Util.Toast("请填写收款事由");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };

    function getList() {
      var param = {};
      param.type = "项目收款类型";
      Util.getKeyValue(param, function (data) {
        $scope.costTypeList = data.data;
        if ($scope.param.receiveSubType) {
          $scope.getcostTypeObj();
        }
      });
    }

    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        if($scope.param.receiveType){
          $scope.param.receiveType=""+$scope.param.receiveType
        }
        getList()
      });
    };
    // 初始化
    init();
  });
